﻿#include "sysinfo.h"
#include "sigar.h"
#include <string.h>
#include <windows.h>
#include <iostream>
using namespace std;

sysinfo::sysinfo()
{

}

int sysinfo::test_sysinfo()
{
    demo_sys_info info;
    getSysInfo(info);

    double uptime;
    getUpTime(uptime);

    cout << uptime << endl;

    return 0;
}

void sysinfo::getSysInfo(demo_sys_info &info)
{
    memset(&info, 0 ,sizeof(demo_sys_info)); /* 置空结构体 */

    sigar_t *sigar;
    sigar_open(&sigar);

    sigar_sys_info_t sysinfo;
    sigar_sys_info_get(sigar, &sysinfo);

    // 结构体赋值
    strncpy(info.name, sysinfo.name, strlen(sysinfo.name));
    strncpy(info.version, sysinfo.version, strlen(sysinfo.version));
    strncpy(info.arch, sysinfo.arch, strlen(sysinfo.arch));
    strncpy(info.machine, sysinfo.machine, strlen(sysinfo.machine));
    strncpy(info.description, sysinfo.description, strlen(sysinfo.description));
    strncpy(info.patch_level, sysinfo.patch_level, strlen(sysinfo.patch_level));
    strncpy(info.vendor, sysinfo.vendor, strlen(sysinfo.vendor));
    strncpy(info.vendor_version, sysinfo.vendor_version, strlen(sysinfo.vendor_version));
    strncpy(info.vendor_name, sysinfo.vendor_name, strlen(sysinfo.vendor_name));
    strncpy(info.vendor_code_name, sysinfo.vendor_code_name, strlen(sysinfo.vendor_code_name));

    sigar_close(sigar);
}

void sysinfo::getUpTime(double &uptime)
{
    sigar_t *sigar;
    sigar_open(&sigar);

    sigar_uptime_t time;
    sigar_uptime_get(sigar, &time);

    uptime = time.uptime;

    sigar_close(sigar);
}

